"""
查找替换 from_unixtime 转换的
"""
import re
import logging


def replace_simple_from_unix_day_hyphen(m):
    logging.debug("匹配到了 「 {}({} / 1000,'%Y-%m-%d') 」".format(m.group(1), m.group(2)))
    new_date_format = "TO_CHAR(TO_TIMESTAMP({} / 1000),'yyyy-mm-dd')".format(m.group(2))
    return new_date_format


def replace_simple_from_unix_day(m):
    logging.debug("匹配到了 「 {}({} / 1000,'%Y%m%d') 」".format(m.group(1), m.group(2)))
    new_date_format = "TO_CHAR(TO_TIMESTAMP({} / 1000),'yyyyMMdd')".format(m.group(2))
    return new_date_format


def replace_simple_from_unixtime(all_the_text):
    logging.info("---- replace_simple_from_unixtime ----")
    unix_timestamp_patten = re.compile(r'[^\(](FROM_UNIXTIME|from_unixtime)\((.*)\/1000\,\'\%Y\-\%m\-\%d\'\)')
    result = unix_timestamp_patten.sub(replace_simple_from_unix_day_hyphen, all_the_text)
    all_the_text = result

    unix_timestamp_patten = re.compile(r'[^\(](FROM_UNIXTIME|from_unixtime)\((.*)\/1000\,\'\%Y\%m\%d\'\)')
    result = unix_timestamp_patten.sub(replace_simple_from_unix_day, all_the_text)
    all_the_text = result

    return all_the_text
